package org.gikolet.bbs.ui;

import org.gikolet.bbs.data.Bbs;
import org.gikolet.bbs.data.Board;
import org.gikolet.bbs.data.BoardList;
import org.gikolet.bbs.imona.reader.IMonaBoardReader;
import org.gikolet.framework.inject.Injector;
import org.gikolet.framework.ui.Container;
import org.gikolet.framework.ui.List;
import org.gikolet.framework.ui.Menu;
import org.gikolet.framework.ui.MenuItem;
import org.gikolet.framework.ui.OptionPane;
import org.gikolet.framework.ui.Tree;
import org.gikolet.framework.ui.TreeNode;
import org.gikolet.framework.ui.events.ActionEvent;
import org.gikolet.framework.ui.events.ActionListener;
import org.gikolet.framework.ui.events.KeyEvent;
import org.gikolet.framework.ui.events.KeyEventListener;
import org.gikolet.framework.util.Assert;
import org.gikolet.framework.view.AbstractView;
import org.gikolet.framework.view.DefaultMenuManager;
import org.gikolet.framework.view.MenuListener;
import org.gikolet.framework.view.MenuManager;

public class BoardListView extends AbstractView {

    private Injector injector;

    private Bbs bbs;

    private Tree tree;

    private BoardList boardList;

    private MenuItem boardUpdateMenu;

    public BoardListView() {
	this.boardList = new BoardList();
    }

    public void service(Injector injector) {
	this.injector = injector;
    }

    protected Injector getInjector() {
	Assert.isNotNull(this.injector);

	return this.injector;
    }

    public Bbs getBbs() {
	return this.bbs;
    }

    public void setBbs(Bbs bbs) {
	this.bbs = bbs;
    }

    public void update() {
    }

    private void load() {
	if (this.tree != null) {

	    TreeNode root = new TreeNode(this.bbs.getName());

	    for (int i = 0; i < this.boardList.getCategoryCount(); i++) {
		String category = this.boardList.getCategory(i);
		TreeNode categoryNode = new TreeNode(category);

		root.addTreeNode(categoryNode);

		int boardCount = this.boardList.getBoardCount(category);
		for (int j = 0; j < boardCount; j++) {
		    categoryNode.addTreeNode(new TreeNode(this.boardList
			    .getBoard(category, j)));
		}
	    }

	    this.tree.setTreeNode(root);
	}
    }

    public void createPartControl(Container parent) {
	this.tree = new Tree();

	this.tree.addKeyEventListener(new KeyEventListener() {
	    public void keyAction(KeyEvent e) {
		listKeyAction(e);
	    }
	});

	parent.add(this.tree);

	createPopupMenu();

	load();
    }

    private void listKeyAction(KeyEvent e) {
    }

    private void menuAction(MenuItem menuItem) {
	if (menuItem == this.boardUpdateMenu) {
	    // 板一覧の更新
	}
    }

    private void selected() {
	// 板を選択
    }

    private void createPopupMenu() {
	DefaultMenuManager menuMgr = new DefaultMenuManager("板ﾒﾆｭｰ");

	this.boardUpdateMenu = new MenuItem("板一覧更新");

	ActionListener menuActionListener = new ActionListener() {
	    public void actionPerformed(ActionEvent e) {
		BoardListView.this.menuAction((MenuItem) e.getSource());
	    }
	};
	this.boardUpdateMenu.addActionListener(menuActionListener);

	menuMgr.setRemoveAllWhenShown(true);
	menuMgr.addMenuListener(new MenuListener() {
	    public void menuAboutToShow(MenuManager manager) {
		fillContextMenu(manager);
	    }
	});

	Menu menu = menuMgr.getContextMenu();
	this.tree.setPopupMenuItem(menu);

	// registerContextMenu(menuMgr, this.listView);
    }

    private void fillContextMenu(MenuManager menuManager) {
	// menuManager.add(this.boardUpdateMenu);
	// menuManager.add(new GroupMarker("toolBar_group"));
	System.out.println(menuManager);
    }

    public void setFocus() {
	if (this.tree != null) {
	    this.tree.setFocus();
	}
    }
}
